package com.example.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class SelectedActivity extends AppCompatActivity {
    private static final String PREFS_NAME = "SelectedCourses";
    private static final String COURSES_KEY = "courses";
    
    private ListView selectedCoursesList;
    private Button btnShouye;
    private CourseAdapter adapter;
    private List<Course> courses;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selected);

        // 初始化SharedPreferences
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

        // 初始化视图
        selectedCoursesList = findViewById(R.id.selectedCoursesList);
        btnShouye = findViewById(R.id.btn_shouye);

        // 从SharedPreferences加载课程数据
        String coursesJson = sharedPreferences.getString(COURSES_KEY, "[]");
        courses = Course.coursesFromJson(coursesJson);
        
        // 检查是否有新课程传入
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("course")) {
            Course newCourse = (Course) intent.getSerializableExtra("course");
            if (newCourse != null) {
                // 检查是否已经选过这门课
                boolean isAlreadySelected = false;
                for (Course course : courses) {
                    if (course.getTitle().equals(newCourse.getTitle())) {
                        isAlreadySelected = true;
                        break;
                    }
                }
                
                if (!isAlreadySelected) {
                    // 检查时间冲突
                    if (hasTimeConflict(newCourse)) {
                        Toast.makeText(this, "该课程与已选课程时间冲突！", Toast.LENGTH_SHORT).show();
                    } else {
                        courses.add(newCourse);
                        saveCourses(); // 保存更新后的课程列表
                    }
                } else {
                    Toast.makeText(this, "您已经选过这门课程了", Toast.LENGTH_SHORT).show();
                }
            }
        }

        // 设置适配器
        adapter = new CourseAdapter(this, courses, new CourseAdapter.OnDropCourseListener() {
            @Override
            public void onDropCourse(int position) {
                // 处理退课逻辑
                Course course = courses.get(position);
                courses.remove(position);
                adapter.notifyDataSetChanged();
                saveCourses(); // 保存更新后的课程列表
                Toast.makeText(SelectedActivity.this, 
                    "已退选课程：" + course.getTitle(), 
                    Toast.LENGTH_SHORT).show();
            }
        });
        selectedCoursesList.setAdapter(adapter);

        // 设置首页按钮点击事件
        btnShouye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 返回首页
            }
        });
    }

    // 检查新课程是否与已选课程时间冲突
    private boolean hasTimeConflict(Course newCourse) {
        for (Course course : courses) {
            if (course.hasTimeConflict(newCourse)) {
                return true;
            }
        }
        return false;
    }

    // 保存课程列表到SharedPreferences
    private void saveCourses() {
        String coursesJson = Course.coursesToJson(courses);
        sharedPreferences.edit()
            .putString(COURSES_KEY, coursesJson)
            .apply();
    }
}